﻿using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

public static class MVCCreateTool
{
    private static List<GameObject> selectables = new List<GameObject>();
    private static List<string> strCode = new List<string>();
    //private static GameObject parent = null;

    private static string direPath = "Assets/";
    private static string copyBind = "";

    [MenuItem("Assets/[FutureCore]/MVC/复制预设绑定组件代码", false, -121)]
    private static void CopyBindUICom()
    {
        SetCopyDefine();
        SetCopyBindUICom();
    }

    [MenuItem("Assets/[FutureCore]/MVC/复制组件绑定方法代码", false, -120)]
    private static void CopyBindMethod()
    {
        SetCopyBindMethod();
    }

    [MenuItem("Assets/[FutureCore]/MVC/创建数据层", false, -102)]
    private static void CreateModelScripts()
    {
        SetModelCodeStr();
    }

    [MenuItem("Assets/[FutureCore]/MVC/创建视图层", false, -101)]
    private static void CreateViewScripts()
    {
        SetViewCodeStr();
    }

    [MenuItem("Assets/[FutureCore]/MVC/创建控制层", false, -100)]
    private static void CreateControllerScripts()
    {
        SetControllerCodeStr();
    }

    //[MenuItem("Assets/[FutureCore]/MVC/Create ALL MVC", false, 103)]
    //static void CreateMVCScripts()
    //{
    //    SetViewCodeStr();
    //    SetModelCodeStr();
    //    SetControllerCodeStr();
    //}

    #region [TOOL]

    private static string GetSelectedPath()
    {
        string selectedPath = "Assets";

        Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.Assets);

        foreach (Object obj in selection)
        {
            selectedPath = AssetDatabase.GetAssetPath(obj);
            if (!string.IsNullOrEmpty(selectedPath) && File.Exists(selectedPath))
            {
                selectedPath = Path.GetDirectoryName(selectedPath);
                break;
            }
        }
        return selectedPath;
    }

    public static void RecursionFind(GameObject go, string name)
    {
        if (go.name.Contains(name))
        {
            selectables.Add(go);
        }

        foreach (Transform child in go.transform)
        {
            RecursionFind(child.gameObject, name);
        }
    }

    private static string addParentStr(GameObject go, string name)
    {
        string str = "";
        strCode.Clear();

        strCode.Add(go.name);
        getParentStr(go, name);

        for (int i = strCode.Count - 1; i >= 0; i--)
        {
            if (i == strCode.Count - 1)
            {
                str += strCode[i];
            }
            else
            {
                str += "/" + strCode[i];
            }
        }
        return str;
    }

    private static void getParentStr(GameObject go, string name)
    {
        if (go.transform.parent.name != name)
        {
            strCode.Add(go.transform.parent.name);
            getParentStr(go.transform.parent.gameObject, name);
        }
    }

    private static string ReplaceNameStr(string name, string name1, string name2)
    {
        return name.Replace(name1, name2);
    }

    #endregion [TOOL]

    #region [Method]

    private static void SetCopyDefine()
    {
        GameObject parent = Selection.activeGameObject as GameObject;
        if (parent == null)
        {
            Debug.Log("Can't find object!");
            return;
        }
        string strDefine = "";
        string strBtn = "";
        string strTxt = "";
        string strSid = "";
        string strImg = "";
        string tab = "   ";
        string strRect = "";

        selectables.Clear();
        RecursionFind(parent, "_Btn");
        for (int i = 0; i < selectables.Count; i++)
        {
            GameObject go = selectables[i];
            string methodName = go.name.Replace("_", "");
            string varName = methodName.Substring(0, 1).ToLower() + methodName.Substring(1, methodName.Length - 1);
            strBtn += "" + tab + "public GameObject " + varName + ";\n";
        }

        selectables.Clear();
        RecursionFind(parent, "_Txt");
        for (int i = 0; i < selectables.Count; i++)
        {
            GameObject go = selectables[i];
            string methodName = go.name.Replace("_", "");
            string varName = methodName.Substring(0, 1).ToLower() + methodName.Substring(1, methodName.Length - 1);
            strTxt += "" + tab + "public Text " + varName + ";\n";
        }

        selectables.Clear();
        RecursionFind(parent, "_Sid");
        for (int i = 0; i < selectables.Count; i++)
        {
            GameObject go = selectables[i];
            string methodName = go.name.Replace("_", "");
            string varName = methodName.Substring(0, 1).ToLower() + methodName.Substring(1, methodName.Length - 1);
            strSid += "" + tab + "public Slider " + varName + ";\n";
        }

        selectables.Clear();
        RecursionFind(parent, "_Img");
        for (int i = 0; i < selectables.Count; i++)
        {
            GameObject go = selectables[i];
            string methodName = go.name.Replace("_", "");
            string varName = methodName.Substring(0, 1).ToLower() + methodName.Substring(1, methodName.Length - 1);
            strImg += "" + tab + "public Image " + varName + ";\n";
        }

        selectables.Clear();
        RecursionFind(parent, "_Rect");
        for (int i = 0; i < selectables.Count; i++)
        {
            GameObject go = selectables[i];
            string methodName = go.name.Replace("_", "");
            string varName = methodName.Substring(0, 1).ToLower() + methodName.Substring(1, methodName.Length - 1);
            strRect += "" + tab + "public RectTransform " + varName + ";\n";
        }

        strDefine = strImg + strTxt + strSid + strBtn + strRect;

        copyBind = tab + "#region Bind\n";
        copyBind += strDefine;
    }

    private static void SetCopyBindUICom()
    {
        GameObject parent = Selection.activeGameObject as GameObject;
        if (parent == null)
        {
            Debug.Log("Can't find object!");
            return;
        }

        string strBind = "";
        string strBtn = "";
        string strTxt = "";
        string strSid = "";
        string strImg = "";
        var tab = "   ";

        selectables.Clear();
        RecursionFind(parent, "_Btn");
        for (int i = 0; i < selectables.Count; i++)
        {
            GameObject go = selectables[i];
            string methodName = go.name.Replace("_", "");
            string varName = methodName.Substring(0, 1).ToLower() + methodName.Substring(1, methodName.Length - 1);

            strBtn += "" + tab + "" + tab + varName + " = rectTransform.Find(\"" + addParentStr(go, parent.name) +
            "\").gameObject; \n" + tab + "" + tab + "UIClickListener.Get(" + varName + ").onClick = OnClick" + methodName + ";";
            strBtn += "\n\n";
        }

        selectables.Clear();
        RecursionFind(parent, "_Txt");
        for (int i = 0; i < selectables.Count; i++)
        {
            GameObject go = selectables[i];
            string methodName = go.name.Replace("_", "");
            string varName = methodName.Substring(0, 1).ToLower() + methodName.Substring(1, methodName.Length - 1);

            strTxt += "" + tab + "" + tab + varName + " = rectTransform.Find(\"" + addParentStr(go, parent.name) + "\").GetComponent<Text>();";
            strTxt += "\n";

            if (i == selectables.Count - 1)
                strTxt += "\n";
        }

        selectables.Clear();
        RecursionFind(parent, "_Sid");
        for (int i = 0; i < selectables.Count; i++)
        {
            GameObject go = selectables[i];
            string methodName = go.name.Replace("_", "");
            string varName = methodName.Substring(0, 1).ToLower() + methodName.Substring(1, methodName.Length - 1);

            strSid += "" + tab + "" + tab + varName + " = rectTransform.Find(\"" + addParentStr(go, parent.name) + "\").GetComponent<Slider>();";
            strSid += "\n";

            if (i == selectables.Count - 1)
                strSid += "\n";
        }

        selectables.Clear();
        RecursionFind(parent, "_Img");
        for (int i = 0; i < selectables.Count; i++)
        {
            GameObject go = selectables[i];
            string methodName = go.name.Replace("_", "");
            string varName = methodName.Substring(0, 1).ToLower() + methodName.Substring(1, methodName.Length - 1);

            strImg += "" + tab + "" + tab + varName + " = rectTransform.Find(\"" + addParentStr(go, parent.name) + "\").GetComponent<Image>();";
            strImg += "\n";

            if (i == selectables.Count - 1)
                strImg += "\n";
        }
        selectables.Clear();
        RecursionFind(parent, "_Rect");
        for (int i = 0; i < selectables.Count; i++)
        {
            GameObject go = selectables[i];
            string methodName = go.name.Replace("_", "");
            string varName = methodName.Substring(0, 1).ToLower() + methodName.Substring(1, methodName.Length - 1);

            strImg += "" + tab + "" + tab + varName + " = rectTransform.Find(\"" + addParentStr(go, parent.name) + "\").GetComponent<RectTransform>();";
            strImg += "\n";

            if (i == selectables.Count - 1)
                strImg += "\n";
        }

        copyBind += "\n" + tab + "private void BindUICom()\n" + tab + "{\n";
        strBind = strImg + strTxt + strSid + strBtn;
        copyBind += strBind;
        copyBind += tab + "}\n" + tab + "#endregion";

        TextEditor te = new TextEditor();
        te.text = copyBind;
        te.SelectAll();
        te.Copy();

        Debug.Log(copyBind);
        Debug.Log("MVCCreateTool: BindUICom 复制完成");
    }

    private static void SetCopyBindMethod()
    {
        GameObject parent = Selection.activeGameObject as GameObject;
        if (parent == null)
        {
            Debug.Log("Can't find object!");
            return;
        }

        string strBtnFuntion = "";
        string tab = "   ";

        selectables.Clear();
        RecursionFind(parent, "_Btn");
        for (int i = 0; i < selectables.Count; i++)
        {
            GameObject go = selectables[i];
            string methodName = go.name.Replace("_", "");

            strBtnFuntion += "" + tab + " private void OnClick" + methodName + "(PointerEventData data)\n" + tab + "{ \n" + tab + "}\n\n";
        }

        TextEditor te = new TextEditor();
        te.text = strBtnFuntion;
        te.SelectAll();
        te.Copy();

        Debug.Log(strBtnFuntion);
        Debug.Log("MVCCreateTool: Bind Method 复制完成！");
    }

    private static void SetControllerCodeStr()
    {
        GameObject parent = Selection.activeGameObject as GameObject;
        if (parent == null)
        {
            Debug.Log("Can't find object!");
            return;
        }
        Debug.Log("create  " + parent.name);

        ControllerCreateScript.objName = parent.name;

        ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0,
            ScriptableObject.CreateInstance<ControllerCreateScript>(),
            direPath + parent.name + "Ctrl.cs", null,
            @"Assets\_OProject\EditorProject\Editor\Tools\AutoProjectTool\MVCTool\CreateMVC.cs.txt");
    }

    private static void SetViewCodeStr()
    {
        GameObject parent = Selection.activeGameObject as GameObject;
        if (parent == null)
        {
            Debug.Log("Can't find object!");
            return;
        }
        Debug.Log("create  " + parent.name);
        string path = AssetDatabase.GetAssetPath(Selection.activeObject);
        int index = path.LastIndexOf("Module");
        string PicPath = path.Substring(index + 7, path.Length - index - 7 - 7);

        ViewCreateScript.objName = parent.name;
        ViewCreateScript.uiPathName = PicPath;

        ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0,
            ScriptableObject.CreateInstance<ViewCreateScript>(),
           direPath + parent.name + ".cs", null,
            @"Assets\_OProject\EditorProject\Editor\Tools\AutoProjectTool\MVCTool\CreateMVC.cs.txt");
    }

    private static void SetModelCodeStr()
    {
        GameObject parent = Selection.activeGameObject as GameObject;
        if (parent == null)
        {
            Debug.Log("Can't find object!");
            return;
        }
        ModelCreateScript.objName = parent.name;

        string modelName = null;
        modelName = parent.name.Substring(0, parent.name.Length - 2);

        ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0,
            ScriptableObject.CreateInstance<ModelCreateScript>(),
            direPath + modelName + "Model.cs", null,
            @"Assets\_OProject\EditorProject\Editor\Tools\AutoProjectTool\MVCTool\CreateMVC.cs.txt");
    }

    #endregion [Method]
}